<?php
namespace app\index\controller;
use think\Db;
class Job
{
    /* 
    * 参数解析 
    * 参数1：jobId：这个是职位ID
    * 参数2：jobName：这个是职位名字
    * 参数3：pageSize：这个是每次查询的数据条数
    * 参数4：currentPage：这是是查找第几页数据
    * */
    public function findJob(){
      $params = $_GET;
			$flag = true;
			$map;
			if($params['jobId'] != null || $params['jobId'] != ""){
				$flag = "false";
				$map['website_job.job_id'] = $params['jobId'];		
			}
			if($params['jobName'] != null || $params['jobName'] != ""){
				$flag = "false";
				$map['website_job.job_name'] =  array('like','%'.$params['jobName'].'%'); 	
	        }
	        if($params['jobDepartment'] != null || $params['jobDepartment'] != ""){
				$flag = "false";
				$map['website_job.job_department'] =  $params['jobDepartment']; 	
			}
			if($flag == "true"){
				$data['data'] = Db::table('website_job')->alias('t1')->join('website_menu t2','t1.job_department = t2.id')->order('create_date desc')->limit($params['pageSize']*$params['currentPage'],$params['pageSize'])->select();
				$data['count'] = Db::table('website_job')->count();
			}else{
				$data['data'] = Db::table('website_job')->alias('t1')->join('website_menu t2','t1.job_department = t2.id')->where($map)->order('create_date desc')->limit($params['pageSize']*$params['currentPage'],$params['pageSize'])->select();
				$data['count'] = Db::table('website_job')->where($map)->count();
			}
        echo json_encode($data);
    }
    /*
	* 这是删除职位的方法 
	* 参数解析
	* 参数1：ids：这个是要删除的职位ID集合格式是（1,2,3）
	*/
	public function deleteJob(){
		$params = $_GET;
		// 这里是把前端的参数转换成一个数组进行操作
		$arr = explode(',',$params['ids']);
		$data['data'] = Db::table('website_job')->delete($arr);
		$data['message'] = "删除成功！";
		$data['status'] = "200";
		echo json_encode($data);
   }
    // 这里保存职位的方法
	public function saveJob(){
		$params = $_GET;
		$data = [
			'job_department'    => $params["jobDepartment"],
			'job_name'          => $params["jobName"],
			'job_duty'          => $params["jobDuty"],
			'job_require'       => $params["jobRequire"],
			'job_address'       => $params["jobAddress"],
			'job_salary'        => $params["jobSalary"],
			'creater'						=> '1',
			'create_date'       => date("Y/m/d H:m:s")
	  ];
	  
   	if($params['jobId']== "" || $params['jobId']== null){
   		$result["data"] = Db::table('website_job')->insert($data);
		}else{
			$result["data"] = Db::table('website_job')->where('job_id', $params['jobId'])->update($data);
		}
    $result["status"] = "200";
    echo  json_encode($result);
  }
    // 这是查找指定Id的职位
	public function jobInfo(){
        $params = $_GET;
		$data['data'] = Db::table('website_job')->where('job_id',$params['id'])->find();
		echo json_encode($data);
	}
    

}
